<?php
class Training_FileController extends Zendvn_Controller_Action{
    
    public function init() {
        parent::init();
        $this->_helper->layout->disableLayout();
        //$this->_helper->viewRenderer->setNoRender();
    }
	//class Zend upload multi file
	public function upload2Action() {
		if($this->_request->isPost()){
			$upload		= new Zendvn_File_Upload();
			$fileName = $upload->getFileName();
			foreach ($fileName as $key => $value) {
				$upload->upload($key, FILES_PATH, array('task'=>'rename'), $key . '_');
			}
		}
	}
//	class Zend upload single file
	public function uploadAction() {
		if($this->_request->isPost()){
			$upload		= new Zendvn_File_Upload();
			echo $upload->upload('picture', FILES_PATH, array('task'=>'rename'));
		}
	}
	
	public function filter2Action() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$info		= $upload->getFileName('picture');
			$fileExtension = pathinfo($info, PATHINFO_EXTENSION);
			$newFileName = 'pic_' . time() . '.' .$fileExtension;
			$options = array('target' => FILES_PATH . '/photo/'. $newFileName);
			$upload->addFilter('Rename', $options, 'picture');
			$upload->receive('picture');
		}
	}		
	//rename file upload
	public function filterAction() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
//			$upload->setDestination(FILES_PATH . '/photo', 'picture');
			$options = array('target' => FILES_PATH . '/photo/picture_2.jpg');
			$upload->addFilter('Rename', $options, 'picture');
			$upload->receive('picture');
		}
	}
	//validate multi file
	public function validate5Action() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(FILES_PATH . '/photo', 'picture');
			$upload->setDestination(FILES_PATH . '/zip', 'filezip');
			
			$upload->addValidator('Extension', false, array('jpg', 'gif', 'png'), 'picture');
			$upload->addValidator('Size', false, array('min'=>100, 'max'=>1000), 'filezip');
			
			$fileName = $upload->getFileName();
			foreach ($fileName as $key => $value) {
				if($upload->isValid($key)){
					$upload->receive($key);
				}else{
					$message = $upload->getMessages();
					echo '<pre>';
					print_r($message);
					echo '</pre>';
				}
			}
		}
    }
	//validate multi file
	public function validate4Action() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(FILES_PATH . '/photo', 'picture');
			$upload->setDestination(FILES_PATH . '/zip', 'filezip');
			
			$upload->addValidator('Extension', false, array('jpg', 'gif', 'png'), 'picture');
			$upload->addValidator('Size', false, array('min'=>100, 'max'=>1000), 'filezip');
			if($upload->isValid()){
				$upload->receive();
			}else{
				$message = $upload->getMessages();
				echo '<pre>';
				print_r($message);
				echo '</pre>';
			}
		}
    }		
		
	//validate
	public function validate3Action() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(FILES_PATH . '/photo');
			
			$upload->addValidator('ExcludeExtension', false, array('jpg', 'gif', 'png'));
			if($upload->isValid()){
				$upload->receive();
			}else{
				$message = $upload->getMessages();
				echo '<pre>';
				print_r($message);
				echo '</pre>';
			}
		}
    }	
	//validate
	public function validate2Action() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(FILES_PATH . '/photo');
			
			$upload->addValidator('Extension', false, array('jpg', 'gif', 'png'))
				   ->addValidator('Size', false, array('min' => 20, 'max' => 20000))
				   ->addValidator('ImageSize', false,
									array('minwidth' => 100,
										  'maxwidth' => 100,
										  'minheight' => 1000,
										  'maxheight' => 1000)
									);
			if($upload->isValid()){
				$upload->receive();
			}else{
				$message = $upload->getMessages();
				echo '<pre>';
				print_r($message);
				echo '</pre>';
			}
		}
    }	
	//validate
	public function validateAction() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(FILES_PATH . '/photo');
			$options = array('jpg', 'gif', 'png');
			$upload->addValidator('Extension', FALSE, $options, 'picture');
			if($upload->isValid()){
				$upload->receive();
			}else{
				$message = $upload->getMessages();
				echo current($message);
			}
		}
    }
	//chon luu file nao
	public function multi4Action() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$file		= 'filezip';
			$info		= $upload->getFileInfo($file);
			$fileSize	= $upload->getFileSize($file);
			$fileName	= $upload->getFileName($file);
			$upload->setDestination(FILES_PATH . '/photo', 'picture');
			$upload->setDestination(FILES_PATH . '/zip', 'filezip');
			$upload->receive('picture');
		}
    }
	//luu file vao 2 thu muc khac nhau
	public function multi3Action() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$file		= 'filezip';
			$info		= $upload->getFileInfo($file);
			$fileSize	= $upload->getFileSize($file);
			$fileName	= $upload->getFileName($file);
			$upload->setDestination(FILES_PATH . '/photo', 'picture');
			$upload->setDestination(FILES_PATH . '/zip', 'filezip');
			$upload->receive();
		}
    }
	
	public function multi2Action() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$file		= 'filezip';
			$info		= $upload->getFileInfo($file);
			$fileSize	= $upload->getFileSize($file);
			$fileName	= $upload->getFileName($file);
			$upload->setDestination(FILES_PATH . '/photo');
			$upload->receive();
		}
    }
	
	public function multiAction() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$file		= 'filezip';
			$info		= $upload->getFileInfo($file);
			$fileSize	= $upload->getFileSize($file);
			$fileName	= $upload->getFileName($file);
			echo '<pre>';
			print_r($fileName);
			echo '</pre>';
		}
    }
	
 	public function index3Action() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(FILES_PATH . '/photo123');
			if($upload->isValid()){
				$upload->receive();
			}else{
				echo 'Co loi xay ra!!!';
			}
		}
    }
	
	public function index2Action() {
		if($this->_request->isPost()){
			$upload		= new Zend_File_Transfer_Adapter_Http();
			$fileSize	= $upload->getFileSize();
			echo '<pre>';
			print_r($fileSize);
			echo '</pre>';
		}
    }
	
    public function indexAction() {
		if($this->_request->isPost()){
			$upload = new Zend_File_Transfer_Adapter_Http();
//			$info	= $upload->getFileInfo();
			$upload->setDestination(FILES_PATH);
			$upload->receive();
//			echo '<pre>';
//			print_r($info);
//			echo '</pre>';
		}
    }
 }
